package com.project.webapp.action.backend.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.project.dao.bean.Limits;
import com.project.service.backend.basic.LimitService;
import com.project.webapp.action.BaseAction;

/**
 * 后台 权限管理 Action 
 * @author Sn
 *
 */
public class LimitAction extends BaseAction 
{

	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = 1L;
	private static final String ADD_MENU = "addLimit";
	private String operateResult = "";
	private LimitService limitService;
	private Limits limits;
	List<Limits> limitList;
	
	@Override
	public String execute() throws Exception 
	{
		limits = new Limits();
		if(limitList!=null && limitList.size()>0)
		{
			limitList.clear();
		}
		return this.query();
	}
	
	public String showAdd() 
	{
		limits = new Limits();
        operateResult = "";
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 查询
     * </pre>
     *
     * @return
     */
    public String query() 
    {
        try 
        {
            operateResult = "";
            limitList = limitService.findLimitBypg(limits);
            System.out.println(limitList.size()+"~~~~~~~~~~");
        } catch (Exception e) 
        {
            e.printStackTrace();
        }

        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 删除
     * </pre>
     *
     * @return
     */
    public String delete() 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        String limitId = req.getParameter("limitId");
        int id = Integer.parseInt(limitId);
        try
        {
        	limits.setLimitId(id);
            
        	limitService.delete(limits);
            limitList = limitService.findLimitBypg(limits);
            operateResult = "suc";
            
        } catch (Exception e) 
        {
            e.printStackTrace();
            operateResult = "fail";
        }
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 编辑
     * </pre>
     *
     * @return
     * @throws Exception 
     */
    public String edit() throws Exception 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        if (limits == null) 
        {
        	limits = new Limits();
        }
        operateResult = "";
        String limitId = req.getParameter("limitId");
        int id = Integer.parseInt(limitId);
        limits.setLimitId(id);
        limits = limitService.getLimitById(limits);
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 保存
     * </pre>
     *
     * @return
     */
    public String save() 
    {
    	try 
    	{
    		limitService.save(limits);
    		operateResult = "suc";
    		
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	limits = new Limits();
    	return ADD_MENU;
    }
    
    public String update() 
    {
    	try
    	{
    		limitService.update(limits);
    		operateResult = "suc";
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	limits = new Limits();
    	return ADD_MENU;
    }
    
	public static String getAddMenu()
	{
		return ADD_MENU;
	}

	public String getOperateResult() {
		return operateResult;
	}

	public void setOperateResult(String operateResult) {
		this.operateResult = operateResult;
	}

	public LimitService getLimitService() {
		return limitService;
	}

	public void setLimitService(LimitService limitService) {
		this.limitService = limitService;
	}

	public Limits getLimits() {
		return limits;
	}

	public void setLimits(Limits limits) {
		this.limits = limits;
	}

	public List<Limits> getLimitList() {
		return limitList;
	}

	public void setLimitList(List<Limits> limitList) {
		this.limitList = limitList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
    
}
